$package('itsm.user');

itsm.user.userMain=function(){
	this.opt='';
	return {
		//列表
		userMainGrid:function(){
			var params = $.extend({},jqGridParams, {	
				url:'user!findPager.action',
				caption:'用户列表',
				colNames:['ID','管理员角色','登录名'],
			 	colModel:[
			 	          {name:'userId',align:'center'},
			 	          {name:'userRole',align:'center'},
			 	          {name:'loginName',align:'left'}
			 	],
				jsonReader: $.extend(jqGridJsonReader, {id: "userId"}),
				sortname:'userId',
				pager:'#userMainPager'
				});
				$("#userMainGrid").jqGrid(params);
				$("#userMainGrid").navGrid('#userMainPager',navGridParams);
				//列表操作项
				$("#t_userMainGrid").css(jqGridTopStyles);
				$("#t_userMainGrid").html($('#userMainGridToolbar').html());
				
				//自适应宽度
				setGridWidth("#userMainGrid","regCenter",10);
		},
		//打开添加窗口
		openAddWin:function(){
			opt='saveUser';
			resetForm('#userMain_add_form');
			$('#userMain_add_win').window('open');
		},
		
		//打开编辑窗口
		openEditWin:function(){
			opt='updatePassword';
			checkBeforeEditGrid('#userMainGrid',function(rowData){
				$.post('user!findUserInfo.action','userDTO.userId='+rowData.userId,function(data){
					$('#userMain_userId').val(data.userId);
					$('#userMain_userRole').val(data.userRole);
					$('#userMain_loginName').val(data.loginName);
					$('#userMain_password').val(data.password);
					$('#userMain_systemPassword').val(data.systemPassword);
					
				});
				$('#userMain_add_win').window('open');
			});	
		},
		
		//保存
		saveUser:function(){
			if($('#userMain_add_win').form('validate')){
				var _param = $('#userMain_add_win form').serialize();
				if(opt=='saveUser'){
					$.post('user!loginNameExist.action','userDTO.loginName='+$('#userMain_loginName').val(),function(result){
						if(result)
							msgAlert('当前登录名已存在!','info');
						else{
							$.post('user!'+opt+'.action',_param,function(data){
								msgShow('保存成功!','show');
								$('#userMain_add_win').window('close');
								$('#userMainGrid').trigger('reloadGrid');
							})
						}
					});
				}else{
					$.post('user!'+opt+'.action',_param,function(data){
						msgShow('保存成功!','show');
						$('#userMain_add_win').window('close');
						$('#userMainGrid').trigger('reloadGrid');
					})
				}
				
			}
		},
		//删除
		deleteUser:function(){
			checkBeforeDeleteGrid('#userMainGrid',function(rowIds){
				var pp = $.param({'ids':rowIds},true);
				$.post("user!deleteUser.action", pp, function(){
					$('#userMainGrid').trigger('reloadGrid');
					msgShow('删除成功!','show');
					
				}, "json");
			});
		},
		//打开搜索窗口
		openSearchWin:function(){
			$('#userMain_search_win').window('open');
		},
		//搜索
		searchUser:function(){
			var sdata=$('#userMain_search_win form').getForm();
			var postData = $("#userMainGrid").jqGrid("getGridParam", "postData");
			$.extend(postData,sdata);
			var _url = 'user!findPager.action';		
			$('#userMainGrid').jqGrid('setGridParam',{url:_url}).trigger('reloadGrid',[{"page":"1"}]);
		},
		init:function(){
			itsm.user.userMain.userMainGrid();
		}
	}
	
}();

$(function(){
	itsm.user.userMain.init();
});